<template>
  <div class="system-overview">
    <!-- 系统架构图 -->
    <el-row class="mb-4">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="card-header">
              <span class="title">工位报警控制终端 - 系统总体架构图</span>
            </div>
          </template>
          
          <div class="architecture-diagram">
            <!-- 物理层 -->
            <div class="layer physical-layer">
              <h3>物理设备层</h3>
              <div class="devices-row">
                <div class="device-group" v-for="area in mockAreas" :key="area.id">
                  <div class="area-title">{{ area.name }}</div>
                  <div class="physical-devices">
                    <div 
                      v-for="device in area.devices" 
                      :key="device.id"
                      class="physical-device"
                    >
                      <!-- 工位设备 -->
                      <div class="workstation">
                        <div class="workstation-name">{{ device.name }}</div>
                        <div class="button-panel">
                          <div class="control-button red-btn">故障</div>
                          <div class="control-button yellow-btn">维护</div>
                          <div class="control-button green-btn">正常</div>
                        </div>
                      </div>
                      
                      <!-- 三色灯 -->
                      <div class="traffic-light-hardware">
                        <div class="light-housing">
                          <div class="light red" :class="{ active: device.status === 'red' }"></div>
                          <div class="light yellow" :class="{ active: device.status === 'yellow' }"></div>
                          <div class="light green" :class="{ active: device.status === 'green' }"></div>
                        </div>
                        <div class="speaker" v-if="device.isAlarming">🔊</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            <!-- 连接线 -->
            <div class="connection-layer">
              <div class="connection-line"></div>
              <div class="connection-label">网络通信 (WebSocket)</div>
            </div>
            
            <!-- 软件层 -->
            <div class="layer software-layer">
              <h3>软件显示层</h3>
              
              <!-- 显示电视机模拟 -->
              <div class="display-screen">
                <div class="screen-header">
                  <span>工位报警控制终端 - 显示电视机</span>
                  <div class="screen-status online">在线</div>
                </div>
                
                <div class="screen-content">
                  <!-- 区域显示 -->
                  <div class="display-areas">
                    <div 
                      v-for="area in mockAreas" 
                      :key="area.id"
                      class="display-area"
                    >
                      <div class="area-header">
                        <span>{{ area.name }}</span>
                        <el-tag :type="getAreaStatus(area)">{{ getAreaStatusText(area) }}</el-tag>
                      </div>
                      
                      <div class="virtual-devices">
                        <div 
                          v-for="device in area.devices" 
                          :key="device.id"
                          class="virtual-device"
                          :class="{ alarming: device.isAlarming }"
                        >
                          <!-- 虚拟三色灯 -->
                          <div class="virtual-traffic-light">
                            <div class="virtual-light red" :class="{ active: device.status === 'red' }"></div>
                            <div class="virtual-light yellow" :class="{ active: device.status === 'yellow' }"></div>
                            <div class="virtual-light green" :class="{ active: device.status === 'green' }"></div>
                          </div>
                          
                          <div class="device-info">
                            <div class="device-name">{{ device.name }}</div>
                            <div class="device-status">{{ getStatusText(device.status) }}</div>
                          </div>
                          
                          <div v-if="device.isAlarming" class="alarm-indicator">⚠️</div>
                        </div>
                      </div>
                    </div>
                  </div>
                  
                  <!-- 报警历史 -->
                  <div class="alarm-history-panel">
                    <h4>实时报警信息</h4>
                    <div class="alarm-list">
                      <div v-for="alarm in mockAlarms" :key="alarm.id" class="alarm-item">
                        <span class="alarm-time">{{ alarm.time }}</span>
                        <span class="alarm-device">{{ alarm.device }}</span>
                        <el-tag :type="alarm.type">{{ alarm.message }}</el-tag>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 技术规格说明 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card>
          <template #header>
            <span>硬件规格</span>
          </template>
          <ul class="spec-list">
            <li>三色LED指示灯 (红/黄/绿)</li>
            <li>声光报警器 (85dB)</li>
            <li>按钮控制面板 (3个按钮)</li>
            <li>工业级外壳 (IP65防护)</li>
            <li>24V直流供电</li>
            <li>RS485/以太网通信</li>
          </ul>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card>
          <template #header>
            <span>软件功能</span>
          </template>
          <ul class="spec-list">
            <li>实时状态同步显示</li>
            <li>多区域设备管理</li>
            <li>报警历史记录</li>
            <li>设备在线监控</li>
            <li>自定义设备命名</li>
            <li>WebSocket实时通信</li>
          </ul>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card>
          <template #header>
            <span>系统特点</span>
          </template>
          <ul class="spec-list">
            <li>物理与虚拟同步显示</li>
            <li>声光双重报警提醒</li>
            <li>区域化设备管理</li>
            <li>实时状态监控</li>
            <li>历史数据追溯</li>
            <li>响应式界面设计</li>
          </ul>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref } from 'vue'

// 模拟数据
const mockAreas = ref([
  {
    id: 1,
    name: '生产区域A',
    devices: [
      { id: 1, name: '工位001', status: 'green', isAlarming: false },
      { id: 2, name: '工位002', status: 'yellow', isAlarming: true },
      { id: 3, name: '工位003', status: 'red', isAlarming: true }
    ]
  },
  {
    id: 2,
    name: '生产区域B',
    devices: [
      { id: 4, name: '工位004', status: 'green', isAlarming: false },
      { id: 5, name: '工位005', status: 'off', isAlarming: false }
    ]
  }
])

const mockAlarms = ref([
  { id: 1, time: '14:25:10', device: '工位003', message: '设备故障', type: 'danger' },
  { id: 2, time: '14:30:25', device: '工位002', message: '维护提醒', type: 'warning' },
  { id: 3, time: '14:35:15', device: '工位001', message: '状态正常', type: 'success' }
])

// 方法
const getStatusText = (status) => {
  const statusMap = {
    red: '故障报警',
    yellow: '维护提醒',
    green: '正常运行',
    off: '离线'
  }
  return statusMap[status] || '未知'
}

const getAreaStatus = (area) => {
  const hasRed = area.devices.some(d => d.status === 'red')
  const hasYellow = area.devices.some(d => d.status === 'yellow')
  
  if (hasRed) return 'danger'
  if (hasYellow) return 'warning'
  return 'success'
}

const getAreaStatusText = (area) => {
  const hasRed = area.devices.some(d => d.status === 'red')
  const hasYellow = area.devices.some(d => d.status === 'yellow')
  
  if (hasRed) return '有故障'
  if (hasYellow) return '需维护'
  return '正常'
}
</script>

<style scoped>
.system-overview {
  padding: 20px;
  background-color: #f5f5f5;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.architecture-diagram {
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 10px;
  color: white;
}

.layer {
  margin-bottom: 30px;
  padding: 20px;
  border-radius: 8px;
}

.physical-layer {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.software-layer {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
}

.layer h3 {
  text-align: center;
  margin-bottom: 20px;
  font-size: 20px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.devices-row {
  display: flex;
  justify-content: space-around;
  gap: 20px;
}

.device-group {
  text-align: center;
}

.area-title {
  font-weight: bold;
  margin-bottom: 15px;
  font-size: 16px;
}

.physical-devices {
  display: flex;
  gap: 15px;
}

.physical-device {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.workstation {
  background: #2c3e50;
  padding: 10px;
  border-radius: 8px;
  min-width: 100px;
}

.workstation-name {
  color: white;
  font-size: 12px;
  margin-bottom: 8px;
  text-align: center;
}

.button-panel {
  display: flex;
  gap: 5px;
}

.control-button {
  width: 25px;
  height: 20px;
  border-radius: 4px;
  font-size: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  cursor: pointer;
}

.red-btn {
  background: #e74c3c;
}

.yellow-btn {
  background: #f39c12;
}

.green-btn {
  background: #27ae60;
}

.traffic-light-hardware {
  background: #34495e;
  padding: 8px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
}

.light-housing {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.light {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1px solid #555;
  opacity: 0.3;
}

.light.red {
  background-color: #e74c3c;
}

.light.yellow {
  background-color: #f39c12;
}

.light.green {
  background-color: #27ae60;
}

.light.active {
  opacity: 1;
  box-shadow: 0 0 10px currentColor;
}

.speaker {
  font-size: 12px;
}

.connection-layer {
  text-align: center;
  margin: 20px 0;
}

.connection-line {
  height: 2px;
  background: linear-gradient(90deg, transparent, #fff, transparent);
  margin: 10px 0;
  position: relative;
}

.connection-line::before {
  content: '↕';
  position: absolute;
  left: 50%;
  top: -10px;
  transform: translateX(-50%);
  background: rgba(255,255,255,0.2);
  padding: 2px 6px;
  border-radius: 4px;
}

.connection-label {
  font-size: 14px;
  opacity: 0.8;
}

.display-screen {
  background: #1a1a1a;
  border: 8px solid #333;
  border-radius: 12px;
  padding: 20px;
  color: #00ff00;
  font-family: 'Courier New', monospace;
}

.screen-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #333;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

.screen-status {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.screen-status.online {
  background: #27ae60;
  color: white;
}

.display-areas {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.display-area {
  border: 1px solid #333;
  padding: 15px;
  border-radius: 6px;
}

.area-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  color: #00ff00;
}

.virtual-devices {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}

.virtual-device {
  background: rgba(0, 255, 0, 0.1);
  border: 1px solid #333;
  padding: 10px;
  border-radius: 4px;
  text-align: center;
  position: relative;
}

.virtual-device.alarming {
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 50% { background: rgba(255, 0, 0, 0.2); }
  51%, 100% { background: rgba(0, 255, 0, 0.1); }
}

.virtual-traffic-light {
  display: flex;
  justify-content: center;
  gap: 4px;
  margin-bottom: 8px;
}

.virtual-light {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1px solid #555;
  opacity: 0.3;
}

.virtual-light.red {
  background-color: #e74c3c;
}

.virtual-light.yellow {
  background-color: #f39c12;
}

.virtual-light.green {
  background-color: #27ae60;
}

.virtual-light.active {
  opacity: 1;
  box-shadow: 0 0 8px currentColor;
}

.device-info {
  color: #00ff00;
}

.device-name {
  font-size: 12px;
  font-weight: bold;
  margin-bottom: 4px;
}

.device-status {
  font-size: 10px;
  opacity: 0.8;
}

.alarm-indicator {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 12px;
}

.alarm-history-panel {
  border-top: 1px solid #333;
  padding-top: 15px;
}

.alarm-history-panel h4 {
  color: #00ff00;
  margin-bottom: 10px;
  font-size: 14px;
}

.alarm-list {
  max-height: 120px;
  overflow-y: auto;
}

.alarm-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 0;
  border-bottom: 1px solid #333;
  font-size: 11px;
}

.alarm-time {
  color: #888;
  min-width: 60px;
}

.alarm-device {
  color: #00ff00;
  min-width: 60px;
}

.spec-list {
  list-style: none;
  padding: 0;
}

.spec-list li {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
  position: relative;
  padding-left: 20px;
}

.spec-list li::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: #67c23a;
  font-weight: bold;
}

.spec-list li:last-child {
  border-bottom: none;
}
</style>